local Entity = {}
Entity.mt = { __index = Entity }
setmetatable(Entity, Entity.mt)

function Entity.initialize( type, deck, x, y )

	local ent = {}
	
	if typeof(deck) == 'table' then 
	
		local a = 1 + 2
	
	end
	
	if deck and deck ~= 'part' and typeof(deck) ~= 'table' then ent.name = deck;ent.deck = ResourceManager:get(deck) 
	else --[[self.deck = ResourceManager:get('base_entity'); ba = true ]]end
	
	
	
	
	if type then ent.type = type else ent.type = 'entity' end
	ent.parent = {}
	
	ent.prop = MOAIProp2D.new()
	ent.prop:setDeck(ent.deck)
	ent.prop:setPriority(100)
	
	
	if deck == 'thruster' or deck == 'airlock_door' 
		or deck == 'control_panel' or deck == 'metal_door'
		or deck == 'wires' or deck == 'waste_tubes'
		or deck == 'air_tubes' or deck == 'supply_tubes'
		or deck == 'water_tubes' then
		local w, h = ent.deck:getSize()
		ent.tile_grid = MOAIGrid.new()
		ent.tile_grid:setSize( 1, 1, TILE_SIZE, TILE_SIZE, -TILE_SIZE*0.5, -TILE_SIZE*0.5 )
		ent.tile_grid:setTile(1, 1, 1)
		ent.prop:setGrid(ent.tile_grid) 	
		
	end
	
	
	
	
	local deck_tidy = {
		["airlock_door"] = function(prop) prop:setScl(1, -1) end,
		["airlock_door_1"] = function(prop) prop:setScl(1, -1) end,
		["airlock_door_2"] = function(prop) prop:setScl(1, -1) end,
		["metal_door"] = function(prop) prop:setScl(1, -1) end,
		["metal_door_1"] = function(prop) prop:setScl(1, -1) end,
		["metal_door_2"] = function(prop) prop:setScl(1, -1) end,
		["control_panel"] = function(prop) prop:setScl(1, -1) end,
		["control_panel_1"] = function(prop) prop:setScl(1, -1) end,
		["control_panel_2"] = function(prop) prop:setScl(1, -1) end,
		["control_panel_3"] = function(prop) prop:setScl(1, -1) end,
		["base_actor"] = function(prop) prop:setScl(1, -1) end,
		["thruster"] = function(prop) prop:setScl(1, -1) end,
		["fuel_cell"] = function(prop) prop:setScl(1, -1) end,
		["wires"] = function(prop) prop:setScl(1, -1) end,
		["air_tubes"] = function(prop) prop:setScl(1, -1) end,
		["waste_tubes"] = function(prop) prop:setScl(1, -1) end,
		["supply_tubes"] = function(prop) prop:setScl(1, -1) end,
		["water_tubes"] = function(prop) prop:setScl(1, -1) end,
		["recycler"] = function(prop) prop:setScl(1, -1) end,
		["food_station"] = function(prop) prop:setScl(1, -1) end,
		["water_tanks"] = function(prop) prop:setScl(1, -1) end,
		["oxygen_tanks"] = function(prop) prop:setScl(1, -1) end,
		["space_toilet"] = function(prop) prop:setScl(1, -1) end,
		["sleep_tube"] = function(prop) prop:setScl(1, -1) end,
		
		
	
	}
	
	ent.deck_name = deck
	if deck_tidy[deck] then
		deck_tidy[deck](ent.prop)
	end
	
	
	--print("entity loc : ", x, y)
	if x and y then
		ent.origin_x = x
		ent.origin_y = y
	
		ent.prop:setLoc(x, y)
	end
	
	--[[
	if parent_package then
		self.prop:setLoc(parent_package.x, parent_package.y)
		if parent_package.prop then			
			print("parent_package ".. parent_package.mode .. " " .. parent_package.prop:getLoc())
			self:setParent( parent_package.prop, parent_package.mode )
		end
	end
	]]
	
	
	-- remappers for tile_maps
	
	ent.dmg_rate = BASE_DAMAGE_RATE/BASE_DAMAGE_RATE
	EntityManager:add_entity(ent)
	
	return ent
	
end

function Entity.new( type, deck, x, y )

	local o = Entity.initialize( type, deck, x, y )
	
	return o

end

function Entity:setLoc(x, y)

	self.prop:setLoc(x, y)

end

function Entity:seekLoc(x, y, length, mode)

	self.prop:seekLoc(x, y, length, mode)

end
--[[
function Entity:setParent( parent, mode )

	setParent( self.prop, parent, mode )
	self.parent_mode = mode

end

]]

function Entity:setParent( parent, mode )

		local action = {
		
			['t'] = function(prop, parent)
				prop:setAttrLink( MOAIProp2D.INHERIT_TRANSFORM, parent, MOAIProp2D.TRANSFORM_TRAIT ) 			
			end,
			
			['c'] = function(prop, parent)
				prop:setAttrLink ( MOAIProp2D.INHERIT_COLOR, parent, MOAIProp2D.COLOR_TRAIT ) 			
			end,
			
			['v'] = function(prop, parent)			
				prop:setAttrLink( MOAIProp2D.ATTR_VISIBLE, parent, MOAIProp2D.ATTR_VISIBLE ) 			
			end,
			
			['tc'] = function(prop, parent)			
				prop:setAttrLink( MOAIProp2D.INHERIT_TRANSFORM, parent, MOAIProp2D.TRANSFORM_TRAIT ) 
				prop:setAttrLink ( MOAIProp2D.INHERIT_COLOR, parent, MOAIProp2D.COLOR_TRAIT ) 			
			end,
			
			['tv'] = function(prop, parent)			
				prop:setAttrLink( MOAIProp2D.INHERIT_TRANSFORM, parent, MOAIProp2D.TRANSFORM_TRAIT ) 
				prop:setAttrLink( MOAIProp2D.ATTR_VISIBLE, parent, MOAIProp2D.ATTR_VISIBLE ) 			
			end,
			
			['tvc'] = function (prop, parent)			
				prop:setAttrLink( MOAIProp2D.INHERIT_TRANSFORM, parent, MOAIProp2D.TRANSFORM_TRAIT ) 
				prop:setAttrLink ( MOAIProp2D.INHERIT_COLOR, parent, MOAIProp2D.COLOR_TRAIT ) 
				prop:setAttrLink( MOAIProp2D.ATTR_VISIBLE, parent, MOAIProp2D.ATTR_VISIBLE ) 			
			end,
			
			['tvcl'] = function(prop, parent)
				prop:setAttrLink( MOAIProp2D.INHERIT_TRANSFORM, parent, MOAIProp2D.TRANSFORM_TRAIT ) 
				prop:setAttrLink ( MOAIProp2D.INHERIT_COLOR, parent, MOAIProp2D.COLOR_TRAIT ) 
				prop:setAttrLink( MOAIProp2D.ATTR_VISIBLE, parent, MOAIProp2D.ATTR_VISIBLE ) 
				prop:setAttrLink( MOAIProp2D.INHERIT_LOC, parent, MOAIProp2D.TRANSFORM_TRAIT )
			
			end,
			
			['vc'] = function (prop, parent)			
				prop:setAttrLink ( MOAIProp2D.INHERIT_COLOR, parent, MOAIProp2D.COLOR_TRAIT ) 
				prop:setAttrLink( MOAIProp2D.ATTR_VISIBLE, parent, MOAIProp2D.ATTR_VISIBLE ) 			
			end,		
		}
	if parent and mode then	
		
		local prop = self:get_prop()
		
		prop:setAttrLink( MOAITransform.INHERIT_TRANSFORM, parent, MOAITransform.TRANSFORM_TRAIT ) 
		--prop:setAttrLink ( MOAIProp2D.INHERIT_COLOR, parent, MOAIProp2D.COLOR_TRAIT ) 
		--prop:setAttrLink( MOAIProp2D.ATTR_VISIBLE, parent, MOAIProp2D.ATTR_VISIBLE ) 
		--prop:setAttrLink( MOAIProp2D.INHERIT_LOC, parent, MOAIProp2D.TRANSFORM_TRAIT )
		--action[mode](prop, parent)		
    end
end

function Entity:getLocalTile()

	local x, y = self.prop:getLoc()
	
	--print("ENTITY getLocalTile x " .. x .. " y " .. y)
	local x2 = x/TILE_SIZE
	local y2 = y/TILE_SIZE
	print("ENTITY getLocalTile x " .. x2 .. " y " .. y2)
	
	return x2, y2

end

function Entity:getLoc()

	return self.prop:getLoc()

end

function Entity:getWorldLoc()

	return self.prop:getWorldLoc()
end

function Entity:clearAttrLink( attrID )
	
	self.prop:clearAttrLink( attrID )

end

function Entity:setAttrLink( attrID, target, targetAttrID )
	
	self.prop:setAttrLink( attrID, target, targetAttrID )

end

function Entity:setAttrLink_e( attrID, target, targetAttrID )
	
	self.prop:setAttrLink( attrID, target.prop, targetAttrID )

end

function Entity:setAttrLink_t( attrID, target, targetAttrID )
	
	self.prop:setAttrLink( attrID, target.transform, targetAttrID )

end

function Entity:get_prop()
	local prop = self.prop
	return prop

end

function Entity:get_ship()

	local id = self:get_ship_id()

	if id then
	
		return ConstructManager.ships[id]
	end

end

function Entity:get_ship_id()

	return self.ship_id

end

function Entity:modelToWorld( x, y)
	
	local ax, ay = self.prop:getLoc()
	local xx = x or ax
	local yy = y or ay
	local x1, y1 = self.prop:modelToWorld(xx, yy)
	
	return x1, y1

end



return Entity